package com.heer.gymnasium.module.store.views;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.ListView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseRefreshActivity;
import com.heer.gymnasium.module.common.beans.GlobeBean;
import com.heer.gymnasium.module.main.views.MainActivity;
import com.heer.gymnasium.module.store.beans.CourseBean;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 集体课 外层列表
 * Created by zy on 2018/1/25.
 */

public class CourseListActivity extends BaseRefreshActivity {
    private final static int RES_ID_ROOT = R.layout.layout_course_list;
    private final static int RES_ID_LIST_VIEW = R.id.layout_course_list_lv;
    private final static int RES_ID_LIST_VIEW_ITEM = R.layout.item_text_with_arrow;
    private final static int RES_ID_LIST_VIEW_ITEM_TV = R.id.item_text_with_arrow_title_tv;


    @BindView(RES_ID_LIST_VIEW)
    ListView listView;

    private CourseListActivity $this;

    private ArrayList<CourseBean> dataList;
    private QuickTypeAdapter<CourseBean> adapter;
    private String storeId;

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_ID_ROOT);
        setRefreshResId(RES_ID_REFRESH_LAYOUT);
        dataList = new ArrayList<>();
        adapter = new QuickTypeAdapter<CourseBean>(this, dataList, RES_ID_LIST_VIEW_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, final CourseBean data, int type, int pos) {
                holder.setText(RES_ID_LIST_VIEW_ITEM_TV, data.getCourseName());
                holder.getParentView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CourseActivity.startActivity($this, data.getCourseId());
                    }
                });
            }
        };
        storeId = getIntent().getStringExtra(EXTRA_ID);
    }

    @Override
    protected void initView() {
        super.initView();
        listView.setAdapter(adapter);
        titleUtils.setTitleText("集体课");
        titleUtils.startMainActivity(this);
    }

    @Override
    protected void loadData(final boolean loadMore) {
        super.loadData(loadMore);
        if (!loadMore) {
            dataList.clear();
        }
        getCourseListByStoreId(storeId);
    }

    private void getCourseListByStoreId(String sid) {
        mAppHelper.apiService().getCourseList(sid).enqueue(new BaseCallback<ApiResponse<List<GlobeBean.Tag>>>() {
            @Override
            public void onSuccess(Call<ApiResponse<List<GlobeBean.Tag>>> call, Response<ApiResponse<List<GlobeBean.Tag>>> response) {
                initCourseList(CourseBean.dataChange(response.body().getData()));
            }
        });
    }

    private void initCourseList(List<CourseBean> courseBeans) {
        dataList.addAll(courseBeans);
        adapter.notifyDataSetChanged();
        setRefresh(false);
    }

    private void setRefresh(boolean flag) {
        if (layoutRefresh != null) {
            layoutRefresh.setRefreshing(flag);
        }
    }

    public static void startActivity(Context context, String id) {
        Intent mIntent = new Intent(context, CourseListActivity.class);
        mIntent.putExtra(EXTRA_ID, id);
        context.startActivity(mIntent);
    }
}
